﻿using CK.Sprite.Form.Core;
using CK.Sprite.Form.Mssql.Domain.DesignTime;
using CK.Sprite.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CK.Sprite.Form.Mssql
{
    public class MssqlConnectionProvider : IFormConnectionProvider
    {
        public IDbConnection CreateDbConnection(string strConn)
        {
            return new SqlConnection(strConn);
        }

        public IAutoBusinessDbService GetAutoBusinessDbService(IUnitOfWork unitOfWork)
        {
            return new MssqlAutoBusinessDb(unitOfWork);
        }

        public T GetRepository<T>(IUnitOfWork unitOfWork) where T : class, IRepository
        {
            if (typeof(T).IsAssignableFrom(typeof(ISpriteObjectRepository)))
            {
                return new SpriteObjectRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(IObjectMethodRepository)))
            {
                return new ObjectMethodRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(IRuntimeRepository)))
            {
                return new MssqlRuntimeRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(ISpriteViewRepository)))
            {
                return new SpriteViewRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(ISpriteFormRepository)))
            {
                return new SpriteFormRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(ISpriteCommonRepository)))
            {
                return new SpriteCommonRepository(unitOfWork) as T;
            }

            if (typeof(T).IsAssignableFrom(typeof(IDictItemRepository)))
            {
                return new DictItemRepository(unitOfWork) as T;
            }

            return default(T);
        }
    }
}
